from rest_framework.generics import GenericAPIView
from rest_framework import serializers
from honse.models import Contract

from rest_framework.mixins import ListModelMixin, CreateModelMixin, RetrieveModelMixin, DestroyModelMixin, UpdateModelMixin


class ContractSerializer(serializers.ModelSerializer):
    class Meta:
        model = Contract
        # fields = ['city_name', 'area_name']
        fields = "__all__"


class ContractView(GenericAPIView, ListModelMixin, CreateModelMixin):
    queryset = Contract.objects.all()
    serializer_class = ContractSerializer

    def get(self, request):

        return self.list(request)

    def post(self, request):
        return self.create(request)


class ContractDetailView(GenericAPIView, DestroyModelMixin, RetrieveModelMixin, UpdateModelMixin):
        queryset = Contract.objects.all()
        serializer_class = ContractSerializer

        def get(self, request, pk):  # url的pk名
            return self.retrieve(request)

        def delete(self, request, pk):
            return self.destroy(request)

        def put(self, request, pk):
            # put请求
            return self.update(request)